/*
 * Created on Wed Jun 06 2018 by Ronnie Ren (zhaosong.ren) from Accenture
 *
 * Copyright (c) 2018 DingXing information & technology limited company.
 */

import { Injectable } from "@angular/core";
// import { UserService } from "../models/transform.service";
import { SqliteService } from "../../../services/sqliteDB/sqlite.service";
// import { BillService } from "./bill.service";

import {JsonMappingUtil} from "../../json-mapping-util";
import {InspectionTasks} from '../../transform/inspection/inspection-tasks';
import {InspectionTasksDto} from '../../dto/inspection/inspection-tasks.dto';
import { CurdService } from "../curd.service";
import { Observable } from "rxjs";
import { InspectionRecordsService } from "./inspection-records.service";
import { InspectionOperationsTickets } from "../../transform/inspection/inspection-operation-tickets";
import { InspectionManuals } from "../../transform/inspection/inspection-manuals";
import { InspectionOperationsTicketsService } from "./inspection-operation-tickets.service";
import { InspectionManualsService } from "./inspection-manuals.service";
import { SyncUtilService } from "../../../services/offline/sync.util.service";
import { WorkTicketService } from "./work-ticket.service";



@Injectable()
export class InspectionTasksService {

    constructor(
        private sqliteService: SqliteService,
        private curdService: CurdService,
        private inspectionRecordsService: InspectionRecordsService,
        private inspectionOperationsTicketsService: InspectionOperationsTicketsService,
        private inspectionManualsService: InspectionManualsService,
        private syncUtilService: SyncUtilService,
        private workTicketService: WorkTicketService
    ) {

    }

    getInspectionTask(id: string, fetchRecordsAndTaskAndManuals: boolean = false): Observable<InspectionTasks> {
        return this.curdService.observableFetchObjects(InspectionTasksDto, { id: id }).map((lists: Array<{}>) => {
            if (lists && lists.length > 0) {
                let dto = new InspectionTasksDto();
                return JsonMappingUtil.MappingAllProp(lists[0], dto);
            }
            return null;
        }).map((inspectionTasks: InspectionTasksDto) => {
           return inspectionTasks ? this.syncUtilService.toObjectFromDto(inspectionTasks, InspectionTasks) : null;
        })
        .flatMap((task) => {
            // return Observable.combineLatest(this.loadRecords(task), this.loadManuals(task), 
            // this.loadOperationTickets(task))
            // .map(() => task);
            return Observable.combineLatest(this.loadRecords(task), this.loadManuals(task), 
            this.loadOperationTickets(task), this.loadWorkTicket(task))
            .map(() => task);
        });
        

    }

    loadRecords(inspectionTasks: InspectionTasks) {
        return this.inspectionRecordsService.getRecords(inspectionTasks.id).do((record) => inspectionTasks.record = record);
    }

    loadOperationTickets(inspectionTasks: InspectionTasks) {
        return this.inspectionOperationsTicketsService.getOperationTickets(inspectionTasks.id).do(
            (operationsTicket) => inspectionTasks.operationsTicket = operationsTicket);
    }

    loadManuals(inspectionTasks: InspectionTasks) {
        return this.inspectionManualsService.getManuals(inspectionTasks.id).do((manuals) => inspectionTasks.manuals = manuals);
    }

    loadWorkTicket(inspectionTasks: InspectionTasks) {
        return this.workTicketService.getWorkTicket(inspectionTasks.id).do((workTicket) => inspectionTasks.workTicket = workTicket);
    }

}
